package com.yxhl.zoume.core.expresscar.model;

import com.yxhl.zoume.core.func.timepicker.ZMTimePicker;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Locale;

/**
 * 专车出行时间选择器
 */
public class ExpressCarZMTimePicker extends ZMTimePicker {

    private static final String TAG = "ExpressCarZMTimePicker";

    private static final String MONDAY = "Mon";
    private static final String TUESDAY = "Tue";
    private static final String WEDNESDAY = "Wed";
    private static final String THURSDAY = "Thu";
    private static final String FRIDAY = "Fri";
    private static final String SATURDAY = "Sat";
    private static final String SUNDAY = "Sun";


    /**
     * 24小时数集合
     */
    private ArrayList<String> mHour24List;
    /**
     * 60分钟数集合
     */
    private ArrayList<String> mMinute60List;


    public ExpressCarZMTimePicker() {
        super();
        getWholeHourList();
        getWholeMinuteList();
    }

    /**
     * 获取24小时数的集合
     */
    private void getWholeHourList() {
        mHour24List = new ArrayList<>();
        for (int j = 0; j <= 24; j++) {
            if (j < 10 && j >= 0) {
                mHour24List.add("0" + j + "点");
            } else {
                mHour24List.add(j + "点");
            }
        }
    }

    /**
     * 获取60分钟数的集合
     */
    private void getWholeMinuteList() {
        mMinute60List = new ArrayList<>();
        for (int j = 0; j <= 6; j++) {
            switch (j) {
                case 0:
                    mMinute60List.add("00分");
                    break;
                default:
                    mMinute60List.add(j * 10 + "分");
            }

        }
    }

    @Override
    protected ArrayList<String> getOptions1Data() {
        ArrayList<String> option1DataList = new ArrayList<>();


        long todayTimeMillis = System.currentTimeMillis();

        int hour = getCurrentHour(todayTimeMillis);
        int minute = getCurrentMinute(todayTimeMillis);
        if (hour == 23 && minute > 50) {
            todayTimeMillis = todayTimeMillis + 60 * 60 * 24 * 1000;
        }

        long tomorrowTimeMillis = todayTimeMillis + 60 * 60 * 24 * 1000;
        long theDayAfterTomorrowTimeMillis = todayTimeMillis + 60 * 60 * 24 * 1000 * 2;

        option1DataList.add("现在");
        option1DataList.add(getCurrentDate(todayTimeMillis));
        option1DataList.add(getCurrentDate(tomorrowTimeMillis));
        option1DataList.add(getCurrentDate(theDayAfterTomorrowTimeMillis));

        return option1DataList;
    }

    @Override
    protected ArrayList<ArrayList<String>> getOptions2Data() {
        ArrayList<ArrayList<String>> options2DataList = new ArrayList<>();

        if (mOptions1Data != null) {
            int size = mOptions1Data.size();
            if (size > 0) {
                for (int i = 0; i < size; i++) {
                    switch (i) {
                        case 0:
                            ArrayList<String> itemList = new ArrayList<>();
                            itemList.add("");
                            options2DataList.add(itemList);
                            break;
                        case 1:
                            ArrayList<String> itemList1 = new ArrayList<>();
                            long currentTimeMillis = System.currentTimeMillis();
                            int hour = getCurrentHour(currentTimeMillis);

                            //分钟数为50-60时,10分钟之后,是下一个时钟
                            int currentMinute = getCurrentMinute(currentTimeMillis);
                            if (currentMinute >= 50) {   //之前是>  >= test
                                hour++;
                            }

                            for (int j = hour; j <= 24; j++) {
                                if (j < 10 && j >= 0) {
                                    itemList1.add("0" + j + "点");
                                } else {
                                    itemList1.add(j + "点");
                                }

                            }
                            options2DataList.add(itemList1);
                            break;
                        default:
                            options2DataList.add(mHour24List);
                    }
                }
            }
        }

        return options2DataList;
    }

    @Override
    protected ArrayList<ArrayList<ArrayList<String>>> getOptions3Data() {

        ArrayList<ArrayList<ArrayList<String>>> options3DataList = new ArrayList<>();

        ArrayList<ArrayList<String>> arrayListArrayList = new ArrayList<>();
        ArrayList<String> stringArrayList = new ArrayList<>();
        stringArrayList.add("");
        arrayListArrayList.add(stringArrayList);
        options3DataList.add(arrayListArrayList);

        if (mOptions1Data != null) {
            int options1size = mOptions1Data.size();
            if (options1size > 1) {
                for (int i = 1; i < options1size; i++) {
                    ArrayList<ArrayList<String>> arrayArrayList = new ArrayList<>();
                    if (mOptions2Data != null) {
                        int options2Size = mOptions2Data.get(i).size();
                        if (options2Size > 0) {
                            for (int j = 0; j < options2Size; j++) {
                                ArrayList<String> arrayList = new ArrayList<>();
                                switch (j) {
                                    case 0:
                                        int start = getCurrentMinute(System.currentTimeMillis()) / 10;
                                        if (start + 1 < 6) {
                                            for (int k = start + 1; k <= 6; k++) {    //测试提前10分
                                                if (k > 0) {
                                                    arrayList.add(k * 10 + "分");
                                                }
                                            }
                                        } else {
                                            arrayList.addAll(mMinute60List);
                                        }
                                        break;
                                    default:
                                        arrayList.addAll(mMinute60List);
                                }
                                arrayArrayList.add(arrayList);
                            }
                        }
                    }
                    options3DataList.add(arrayArrayList);
                }
            }
        }

        return options3DataList;
    }


    /**
     * 获取当前日期
     *
     * @param currentTimeMillis
     * @return
     */
    private String getCurrentDate(long currentTimeMillis) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日", Locale.US);
        String currentDate = sdf.format(currentTimeMillis);
//        sdf.applyPattern("E");
//        String weekdayName = getWeekdayName(sdf.format(currentTimeMillis));

//        SpannableString ss = new SpannableString();
//        AbsoluteSizeSpan absoluteSizeSpan = new AbsoluteSizeSpan(18);
//        ss.setSpan(absoluteSizeSpan,0,ss.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);

        return currentDate;
    }

    /**
     * 获取当前小时数
     *
     * @param currentTimeMillis
     * @return
     */
    private int getCurrentHour(long currentTimeMillis) {
        int ret;
        SimpleDateFormat sdf = new SimpleDateFormat("HH", Locale.US);
        try {
            ret = Integer.parseInt(sdf.format(currentTimeMillis));
        } catch (NumberFormatException e) {
            e.printStackTrace();
            ret = 0;
        }
        return ret;
    }

    /**
     * 获取当前分钟数
     *
     * @param currentTimeMillis
     * @return
     */
    private int getCurrentMinute(long currentTimeMillis) {
        int ret;
        SimpleDateFormat sdf = new SimpleDateFormat("mm", Locale.US);
        try {
            ret = Integer.parseInt(sdf.format(currentTimeMillis));
        } catch (NumberFormatException e) {
            e.printStackTrace();
            ret = 0;
        }
        return ret;
    }


    /**
     * 获取对应的星期名
     *
     * @param token
     * @return
     */
    private String getWeekdayName(String token) {
        String ret;
        switch (token) {
            case MONDAY:
                ret = "周一";
                break;
            case TUESDAY:
                ret = "周二";
                break;
            case WEDNESDAY:
                ret = "周三";
                break;
            case THURSDAY:
                ret = "周四";
                break;
            case FRIDAY:
                ret = "周五";
                break;
            case SATURDAY:
                ret = "周六";
                break;
            case SUNDAY:
                ret = "周天";
                break;
            default:
                ret = "";
        }
        return ret;
    }


}
